package View;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import zOthers.Log;
import Controller.AbstractController;

public class ViewFrameMain extends AbstractView {
	private final static String TAG = "ViewFrameMain";

	private final JFrame frameMain;
	private ArrayList<JPanel> panelsList;
	private AbstractController controller;

	/*
	 * Constructors
	 */
	public ViewFrameMain() {
		// Create a frame
		frameMain = new JFrame();
		frameMain.setUndecorated(true);
		frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameMain.setBounds(100, 100, 800, 700);
		frameMain.setResizable(false);
		frameMain.setVisible(true);

		// To make the game full screen

		GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		device.setFullScreenWindow(frameMain);
	}

	public void addPanel(JPanel panel) {
		if(panelsList == null) {
			panelsList = new ArrayList<JPanel>();
		}

		panelsList.add(panel);
	}

	/*
	 * Accessors
	 */
	public JFrame getFrame() {
		return frameMain;
	}

	public AbstractController getController() {
		return controller;
	}

	/*
	 * Mutators
	 */
	public void setController(AbstractController ctrl) {
		this.controller = ctrl;
	}

	/*
	 * Event handler for those events coming from the controller
	 */
	@Override
	public void modelPropertyChange(final PropertyChangeEvent evt) {
		System.out.println(this.getClass().toString() + ".modelPropertyChange() called -- " +
				evt.getPropertyName() + ": " + evt.getOldValue().toString() +
				" -> " + evt.getNewValue().toString());

		Log.info(TAG, "modelPropertyChange() called -- " +
				evt.getPropertyName() + ": " + evt.getOldValue().toString() +
				" -> " + evt.getNewValue().toString());

		if (evt.getPropertyName().equals("panelID")) {
			frameMain.setContentPane(panelsList.get(Integer.parseInt(evt.getNewValue().toString())));
			frameMain.getContentPane().requestFocusInWindow();
			frameMain.setVisible(true);
		}
	}
}
